import { query, remove, add,update} from '../services/dept';
import { message,} from 'antd';

export default {
  namespace: 'dept',

  state: {
    data: [],
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(query, payload);
      if(response.ret){
        yield put({
          type: 'save',
          payload: response.data,
        });
      }
      
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(add, payload);
      if(response.ret){
        message.success(response.msg);
      }else{
        message.error(response.msg);
      }
      yield put({
        type: 'fetch',
        payload: {},
      });
      if (callback) callback();
      
    },
    *update({ payload, callback }, { call, put }) {
      const response = yield call(update, payload);
      if(response.ret){
        message.success(response.msg);
      }else{
        message.error(response.msg);
      }
      yield put({
        type: 'fetch',
        payload: {},
      });
      if (callback) callback();
      
    },
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(remove, payload);
      if(response.ret){
        message.success(response.msg);
      }else{
        message.error(response.msg);
      }
      yield put({
        type: 'fetch',
        payload: {},
      });
      if (callback) callback();
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
  },
};
